package com.xdf.controller;

import com.xdf.annotation.IgnoreResponseAdvice;
import com.xdf.service.JWTService;
import com.xdf.vo.JwtToken;
import com.xdf.vo.UsernameAndPassword;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author chanchaw
 * @create 2025-08-22 10:41
 */
@RequiredArgsConstructor
@CrossOrigin
@RestController
@RequestMapping("/authority")
public class AuthorityController {
    private final JWTService jwtService;

    /**
     * 检查账号+密码，创建并返回 token
     * @param record
     * @return
     * @throws Exception
     */
    // @IgnoreResponseAdvice 取消统一返回对象的封装
    @PostMapping("/loginToken")
    public JwtToken checkPwdAndGetToken(@RequestBody UsernameAndPassword record) throws Exception {
        String s = jwtService.generateToken(record.getUsername(), record.getPassword());
        return new JwtToken(s);
    }

    /**
     * 注册并返回创建的新的 token
     * @param record
     * @return
     * @throws Exception
     */
    // @IgnoreResponseAdvice 取消统一返回对象的封装
    @PostMapping("/registerToken")
    public JwtToken registerAndGetToken(@RequestBody UsernameAndPassword record) throws Exception {
        String s = jwtService.registerUserAndGenerateToken(record);
        return new JwtToken(s);
    }
}
